package com.eclink.iot.tcp.virtualdevice;

import com.eclink.iot.tcp.virtualdevice.entity.Device;
import com.eclink.iot.tcp.virtualdevice.enums.CommandTypeEnum;
import com.eclink.iot.tcp.virtualdevice.enums.FrameAPIEnum;
import com.eclink.iot.tcp.virtualdevice.enums.VirtualDeviceStatusEnum;
import com.eclink.iot.tcp.virtualdevice.util.SocketUtil;
import io.vertx.core.Vertx;
import io.vertx.core.net.NetClient;
import io.vertx.core.net.NetClientOptions;
import io.vertx.core.net.NetSocket;
import lombok.Data;
import org.springframework.beans.BeanUtils;

@Data
public class VirtualDevice {

    private Integer id;

    private String name;

    private String uuid;

    private String host;

    private Integer port;

    private String version;

    private long runtime;

    private NetClient netClient;

    private NetSocket netSocket;

    private int status;

    public VirtualDevice(Device device) {
        // 把设备数据复制进来
        BeanUtils.copyProperties(device, this);

        // 创建网络连接
        Vertx vertx = Vertx.vertx();
        NetClientOptions options = new NetClientOptions()
                .setConnectTimeout(2000);

        this.netClient = vertx.createNetClient(options);
        this.status = VirtualDeviceStatusEnum.INIT.getValue();
    }

    public void connect() {

        this.status = VirtualDeviceStatusEnum.CONNECTING.getValue();
        this.netClient.connect(this.port, this.host, res -> {  //handler with a result
            if (res.succeeded()) {
                this.status = VirtualDeviceStatusEnum.CONNECTED.getValue();
                System.out.println("Connected!");
                NetSocket socket = res.result();
                this.netSocket = socket;
                SocketUtil.ping(socket, this.uuid);
                socket.handler(buffer -> {
                    byte[] bytes = buffer.getBytes();
                    byte frameApi = bytes[1];
                    byte command = bytes[2];

                    // 服务器发送了命令消息
                    if (frameApi == FrameAPIEnum.COMMAND.getValue()) {
                        System.out.println("收到命令。。。。" + command);
                        // 状态命令
                        if (command == CommandTypeEnum.STATUS.getValue()) {
                            System.out.println("收到状态命令。。。。");
                            SocketUtil.ping(socket, this.uuid);
                        } else if (command == CommandTypeEnum.INVENTORY.getValue() || command == CommandTypeEnum.INVENTORY_MULTI.getValue()) {
                            System.out.println("收到盘点命令。。。。");
                            SocketUtil.sendRfid(socket, this.uuid, "AAAA");
                        }
                    }
                    // 服务器发送了回应消息
                    if (frameApi == FrameAPIEnum.RESPONSE.getValue()) {
                        // 先不处理
                        System.out.println("收到服务器回应消息");
                    }

                });
            } else {
                this.status = VirtualDeviceStatusEnum.DISCONNECTED.getValue();
                System.out.println("Failed to connect: " + res.cause().getMessage());
            }
        });
    }

    public void sendTag(String tag) {
        SocketUtil.sendRfid(this.netSocket, this.uuid, tag);
    }




}
